package test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.hadoop.http.HttpServer;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.jetty.webapp.WebAppContext;

public class AppServer {

	public static void startServer() throws Exception {
		Server server = new Server();
		
		Connector listener = HttpServer.createDefaultChannelConnector();
		listener.setHost("127.0.0.1");
		listener.setPort(9595);
		server.addConnector(listener);
		
	    ContextHandlerCollection contexts = new ContextHandlerCollection();
	    server.setHandler(contexts);

		WebAppContext webAppContext = new WebAppContext();
	    webAppContext.setDisplayName("WepAppsContext");
	    webAppContext.setContextPath("/");
		webAppContext.addServlet( new ServletHolder(new BpServlet()), "/bpservlet");
		server.addHandler(webAppContext);
		
		server.start();
		server.join();
	}
	
	public static void main(String[] args) throws Exception {
		startServer();
	}
}

class BpServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// 接受参数
		String key = request.getParameter("keywords");
		// 业务逻辑
		//
		// 返回内容
		response.setCharacterEncoding("utf-8");
		response.setStatus(HttpServletResponse.SC_OK);
		PrintWriter out = response.getWriter();
		out.print("Welcome to Lifeba");
		out.flush();
		out.close();
	}
}